package com.cn.jinl.controller;

import com.cn.jinl.api.IMaterialService;
import com.cn.jinl.domain.Material;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.JsonResponse;
import com.cn.jinl.utils.QueryForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created On : 2023/12/5.
 * <p>
 * Author : Huiyan
 * <p>
 * Description:基础数据-----参数管理----物料管理控制层
 */
@Controller
@RequestMapping("/material")
public class MaterialController {

    @Autowired
    private IMaterialService materialService;

    /**
     * @author : Huiyan feng
     * @date   : 2023/12/5
     * @param  : [queryForm, frontToken]
     * @return : com.cn.jinl.utils.JsonResponse<java.util.List<com.cn.jinl.domain.Material>>
     * @description : getMaterialList 初始化页面查询列表
     */
    @RequestMapping("/getAllMaterialList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Material>> getAllMaterialList(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<Material>> response = new JsonResponse<>();
        List<Material> materials = materialService.getAllMaterialList();
        response.setResult(materials);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getMaterialWithItemName/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Material>> getMaterialWithItemName(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<Material>> response = new JsonResponse<>();
        List<Material> materials = materialService.getMaterialWithItemName();
        response.setResult(materials);
        response.setSuccess(true);
        return response;
    }


    /**
     * @author : Huiyan feng
     * @date   : 2023/12/5
     * @param  : [queryForm, frontToken]
     * @return : com.cn.jinl.utils.JsonResponse<java.util.List<com.cn.jinl.domain.Material>>
     * @description : getMaterialSelect 根据条件查询物料数据
     */
    @RequestMapping("/getMaterialSelect/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Material>> getMaterialSelect(@RequestBody QueryForm queryForm,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<Material>> response = new JsonResponse<>();
        List<Material> materials = materialService.getMaterialSelect(queryForm);
        response.setResult(materials);
        response.setSuccess(true);
        return response;
    }

    /**
     * @author :Chenxingliang
     * @date   : 2024/1/7
     * @param  : [queryForm, frontToken]
     * @return : com.cn.jinl.utils.JsonResponse<java.util.List<com.cn.jinl.domain.Material>>
     * @description : getSingleMaterialSelect 下拉选择
     */
    @RequestMapping("/getSingleMaterialSelect/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Material>> getSingleMaterialSelect(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<Material>> response = new JsonResponse<>();
        List<Material> materials = materialService.getSingleMaterialSelect();
        response.setResult(materials);
        response.setSuccess(true);
        return response;
    }

    /**
     * @author : Huiyan feng
     * @date   : 2023/12/5
     * @param  : [material, frontToken]
     * @return : java.lang.String
     * @description : updateMaterial 修改物料数据
     */
    @RequestMapping("/updateMaterial/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateMaterial(@RequestBody Material material, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = materialService.updateMaterial(material);
        return CommonUtil.getResponseData(response,result);
    }

    /**
     * @author : Huiyan feng
     * @date   : 2023/12/5
     * @param  : [queryForm, frontToken]
     * @return : com.cn.jinl.utils.JsonResponse<java.lang.String>
     * @description : deleteMaterial 删除选中的物料数据
     */
    @RequestMapping("/deleteMaterial/{frontToken}")
    @ResponseBody
    public JsonResponse<String> deleteMaterial(@RequestBody QueryForm queryForm,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = materialService.deleteMaterial(queryForm);
        return CommonUtil.getResponseData(response,result);
    }


    /**
     * @author : Huiyan feng
     * @date   : 2023/12/5
     * @param  : [materials, frontToken]
     * @return : com.cn.jinl.utils.JsonResponse<java.lang.String>
     * @description : addMaterial 增加物料信息
     */
    @RequestMapping("/addMaterial/{frontToken}")
    @ResponseBody
    public JsonResponse<String> addMaterial(@RequestBody List<Material> materials,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = materialService.addMaterial(materials);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/getMaterialByGroupBigTypeId/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,List<Material>>> getMaterialByGroupBigTypeId(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<Map<String,List<Material>>> response = new JsonResponse<>();
        Map<String,List<Material>> result = materialService.getMaterialByGroupBigTypeId();
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

}
